package com.tekj.cxqcb.view.Module.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.qw.soul.permission.SoulPermission;
import com.qw.soul.permission.bean.Permission;
import com.qw.soul.permission.bean.Permissions;
import com.qw.soul.permission.callbcak.CheckRequestPermissionsListener;
import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.config.enums.EventBusMsgBean;
import com.tekj.cxqcb.view.user.LoginActivity;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.plugins.tbs.WebCoreAction;
import common.tool.PUB;
import common.tool.Tools;
import es.dmoral.toasty.Toasty;

/**
 * 设置界面
 */
public class SystemActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.ll_Account)
    LinearLayout llAccount;
    @BindView(R.id.ll_Address)
    LinearLayout llAddress;
    @BindView(R.id.ll_Feedback)
    LinearLayout llFeedback;
    @BindView(R.id.ll_agreement)
    LinearLayout llAgreement;
    @BindView(R.id.ll_Privacy)
    LinearLayout llPrivacy;
    @BindView(R.id.ll_Clear)
    LinearLayout llClear;
    @BindView(R.id.img_logo)
    ImageView imgLogo;
    @BindView(R.id.tv_out)
    TextView tvOut;
    @BindView(R.id.tv_clear)
    TextView tvClear;

    @Override
    public void init() {
        tvTitle.setText("设置");
        try {
            tvClear.setText(Tools.getTotalCacheSize(getApplication()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int setLayout() {
        return R.layout.activity_system;
    }

    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

    @Override
    public void initBindingView(Object mBean) {

    }


    @OnClick({R.id.ll_back, R.id.ll_Account, R.id.ll_Address,
            R.id.ll_Feedback, R.id.ll_agreement, R.id.ll_Clear,
            R.id.tv_out, R.id.ll_Privacy})
    public void onViewClicked(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.ll_Account:
                //TODO 账号与安全
                intent = new Intent(activity, AccountActivity.class);
                break;
            case R.id.ll_Address:
                //TODO 地址
                SoulPermission.getInstance().checkAndRequestPermissions(
                        Permissions.build(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION),
                        // if you want do noting or no need all the callbacks you may use SimplePermissionsAdapter instead
                        new CheckRequestPermissionsListener() {
                            @Override
                            public void onAllPermissionOk(Permission[] allPermissions) {
                                activity.startActivity(new Intent(activity, StoreAddressActivity.class));
                            }

                            @Override
                            public void onPermissionDenied(Permission[] refusedPermissions) {
                                Toasty.normal(activity, "缺少定位权限！").show();
                            }
                        });
                break;
            case R.id.ll_Feedback:
                //TODO 反馈
                intent = new Intent(activity, FeedbackActivity.class);
                break;
            case R.id.ll_agreement:
                //TODO 用户协议
                new WebCoreAction(activity, "https://cars-cxqc.com/static/dist/index.html#/agreement1");
                break;
            case R.id.ll_Privacy:
                //TODO 隐私策略
                new WebCoreAction(activity, "https://cars-cxqc.com/static/dist/index.html#/agreement");
                break;
            case R.id.ll_Clear:
                //TODO 清除缓存
                Tools.showNormalDialogOne(activity, "清除缓存", "您确定要清除缓存么", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Tools.clearAllCache(getApplication());
                        try {
                            tvClear.setText(Tools.getTotalCacheSize(getApplication()));
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toasty.error(getApplicationContext(), "获取缓存失败！").show();
                        }
                    }
                }, null);
                break;
            case R.id.tv_out:
                //TODO 退出登录

                Tools.showNormalDialogOne(activity, "温馨提醒", "您确定要退出当前账号么", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        MainBActivity.User = null;
                        EventBus.getDefault().post(new EventBusMsgBean(EventBusMsgBean.EnumStatus.退出登陆刷新, ""));

                        PUB.SharedPreferences(activity, "mobile", "");
                        PUB.SharedPreferences(activity, "userid", "");
                        PUB.SharedPreferences(activity, "token", "");

                        startActivity(new Intent(activity, LoginActivity.class));
                        finish();
                    }
                }, null);

                break;
        }
        if (intent != null) {
            startActivity(intent);
        }
    }
}
